﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EDXLSharp.EDXLTEPLib;
using System.Xml;

namespace EDXLSharp.EDXLTEPLib.Test
{
  [TestClass]
  public class TEPTest
  {
    [TestMethod]
    public void TEPParseAndSerializeTest()
    {
      string xml = baseTEP("TEPMessage");
      EDXLTEP tep = new EDXLTEP();
      XmlNode node = getXmlNode(xml);
      tep.ReadXML(node);
    }

    private XmlNode getXmlNode(string xmlData)
    {
      XmlDocument doc = new XmlDocument();
      try
      {
        doc.LoadXml(xmlData);
      }
      catch (XmlException)
      {
        throw new ArgumentException("Invalid XML String");
      }

      XmlNodeList denodelist = doc.GetElementsByTagName("TEPMessage");
      if (denodelist.Count == 0)
      {
        throw new FormatException("No TEPMessage Element found!");
      }
      else if (denodelist.Count > 1)
      {
        throw new FormatException("Multiple TEPMessage Elements found!");
      }

      XmlNode deroot = denodelist[0];

      return deroot;
    }

    private string baseClient(string tagname)
    {
      string client =
        "<" + tagname + ">" +
          "<clientID>" +
            "<ID>12345</ID>" +
            "<source>a place</source>" +
          "</clientID>" +
          "<gender>female</gender>" +
          "<clientAge>" +
            "<age>32</age>" +
            "<estimated>true</estimated>" +
            "<units>years</units>" +
          "</clientAge>" +
          baseSituation("Situation") +
          baseHealthCareProvider("HealthCareProvider") +
          baseClientEncounter("ClientEncounter") +
        "</" + tagname + ">";
      return client;
    }

    private string baseSituation(string tagname)
    {
      string situation =
        "<" + tagname + ">" +
          baseIncidentID("incidentID") +
          baseEDXLLocation("incidentLocation") +
        "</" + tagname + ">";
      return "";
    }

    private string baseHealthCareProvider(string tagname)
    {
      string hcp =
        "<" + tagname + ">" +
        "</" + tagname + ">";
      return hcp;
    }

    private string baseClientEncounter(string tagname)
    {
      string clientEncounter = 
        "<" + tagname + ">" +
        "</" + tagname + ">";
      return clientEncounter;
    }

    private string baseIncidentID(string tagname)
    {
      string incidentID =
        "<" + tagname + ">" +
        "</" + tagname + ">";
      return incidentID;
    }

    private string baseEDXLLocation(string tagname)
    {
      string loc =
        "<" + tagname + ">" +
        "</" + tagname + ">";

      return loc;
    }

    private string baseTEP(string tagname)
    {
      string tep =
        "<TEPMessage><messageID>asdf</messageID>" +
        baseClient("Client") + "</TEPMessage>";
      return tep;
    }
  }
}
